package es.mgil.whoWantsToBeAMillonaire.dialogs;

import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.DialogInterface.OnClickListener;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import es.mgil.whoWantsToBeAMillonaire.R;
import es.mgil.whoWantsToBeAMillonaire.activities.Play;

public class CancelDialog extends DialogFragment {
	public static final String CANCEL_DIALOG_ID = "cancel_dialog_id";

	@Override
	public Dialog onCreateDialog(Bundle savedInstanceState) {
		final Play play = (Play) getActivity();

		// first set the runnable state to ASK EXIT
		play.runnable.askingExit();
		AlertDialog.Builder builder = new AlertDialog.Builder(play);
		builder.setMessage(R.string.play_dialog_cancel);
		builder.setPositiveButton(R.string.play_dialog_yes,
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// if want to cancel playing, set the runnable state to
						// CANCELED
						play.runnable.cancelLopping();

						// then finishes the activity
						play.finish();
					}
				});
		builder.setNegativeButton(R.string.play_dialog_no,
				new OnClickListener() {
					@Override
					public void onClick(DialogInterface dialog, int which) {
						// set the runnable state to LOOPING
						play.runnable.continueLooping();
					}
				});
		
		// can't press back button while showing this dialog
		setCancelable(false);
		return builder.create();
	}
}